package com.andaily.domain.dto.account;

import com.andaily.domain.account.Account;
import com.andaily.domain.account.AccountType;
import com.andaily.domain.dto.AbstractDTO;
import com.andaily.domain.shared.DateUtils;
import com.andaily.domain.shared.MobileStyle;
import com.andaily.domain.shared.security.SecurityUtils;
import com.andaily.domain.user.User;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Shengzhao Li
 */
public class AccountDTO extends AbstractDTO {

    private String amount;
    private String accountDate;
    private String description;
    private String typeName;
    private boolean publicity;

    private String createTime;
    private boolean allowEdit;
    private boolean restored;
    private String lastUpdateDate;

    private String creator;
    private boolean income;
    private MobileStyle style;

    public AccountDTO(Account account) {
        super(account.guid());
        this.amount = account.amount().toString();
        this.accountDate = DateUtils.toDateText(account.accountDate());

        AccountType type = account.type();
        this.income = type.isIncome();
        this.typeName = type.name();
        this.style = type.style();
        this.publicity = type.publicity();

        this.description = account.description();
        this.createTime = DateUtils.toDateTime(account.createTime());
        this.lastUpdateDate = DateUtils.toDateTime(account.lastUpdateDate());

        User owner = account.owner();
        String nickName = owner.nickName();
        this.creator = (StringUtils.isNotEmpty(nickName) ? nickName : owner.email());
        this.allowEdit = owner.guid().equals(SecurityUtils.currentUserGuid());
        this.restored = account.restored();
    }

    public boolean isLogin() {
        return SecurityUtils.isLogged();
    }

    public MobileStyle getStyle() {
        return style;
    }

    public void setStyle(MobileStyle style) {
        this.style = style;
    }

    public boolean isIncome() {
        return income;
    }

    public boolean isRestored() {
        return restored;
    }

    public void setRestored(boolean restored) {
        this.restored = restored;
    }

    public boolean isPublicity() {
        return publicity;
    }

    public void setPublicity(boolean publicity) {
        this.publicity = publicity;
    }

    public String getCreator() {
        return creator;
    }

    public String getLastUpdateDate() {
        return lastUpdateDate;
    }

    public void setLastUpdateDate(String lastUpdateDate) {
        this.lastUpdateDate = lastUpdateDate;
    }

    public String getAmount() {
        return amount;
    }

    public void setAmount(String amount) {
        this.amount = amount;
    }

    public String getAccountDate() {
        return accountDate;
    }

    public void setAccountDate(String accountDate) {
        this.accountDate = accountDate;
    }

    public String getDescription() {
        if (StringUtils.isBlank(description)) {
            return "...";
        }
        return StringEscapeUtils.escapeHtml(description);
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getTypeName() {
        return typeName;
    }

    public void setTypeName(String typeName) {
        this.typeName = typeName;
    }

    public boolean isAllowEdit() {
        return allowEdit;
    }

    public void setAllowEdit(boolean allowEdit) {
        this.allowEdit = allowEdit;
    }

    public static List<AccountDTO> toDTOList(List<Account> accountList) {
        List<AccountDTO> list = new ArrayList<AccountDTO>(accountList.size());
        for (Account account : accountList) {
            list.add(new AccountDTO(account));
        }
        return list;
    }

    public String getCreateTime() {
        return createTime;
    }
}
